package cc.codeshop.animator.grid;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import cc.codeshop.animator.R;

public class GridActivity extends Activity implements OnItemClickListener, View.OnClickListener {

	private ImageView imageView;
	private Rect startBounds;
	ObjectAnimator anim;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_grid);

		GridView gridView = (GridView) findViewById(R.id.gridView);
		GridAdapter adapter = new GridAdapter(this);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(this);

		imageView = (ImageView) findViewById(R.id.imageView);
		imageView.setOnClickListener(this); // 若不设置大图事件，则当放大后，不管点哪里都是触发onItemClick()事件
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Log.d("cc", "onItemClick()");
		final ImageView thumbView = (ImageView) view;
		startBounds = new Rect();
		Rect finalBounds = new Rect();
		thumbView.getGlobalVisibleRect(startBounds);
		Point globalBounds = new Point();
		findViewById(R.id.container).getGlobalVisibleRect(finalBounds, globalBounds);
		startBounds.offset(-globalBounds.x, -globalBounds.y);
		finalBounds.offset(-globalBounds.x, -globalBounds.y);

//		final float scaleX = startBounds.width() / finalBounds.width();
//		final float scaleY = startBounds.height() / finalBounds.height();

		float startScale;
		if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds.width() / startBounds.height()) {
			// Extend start bounds horizontally
			startScale = (float) startBounds.height() / finalBounds.height();
			float startWidth = startScale * finalBounds.width();
			float deltaWidth = (startWidth - startBounds.width()) / 2;
			startBounds.left -= deltaWidth;
			startBounds.right += deltaWidth;
		} else {
			// Extend start bounds vertically
			startScale = (float) startBounds.width() / finalBounds.width();
			float startHeight = startScale * finalBounds.height();
			float deltaHeight = (startHeight - startBounds.height()) / 2;
			startBounds.top -= deltaHeight;
			startBounds.bottom += deltaHeight;
		}

		imageView.setImageResource(R.drawable.photo_small);
		imageView.setVisibility(View.VISIBLE);

		// 默认是target的中间位置
		imageView.setPivotX(0);
		imageView.setPivotY(0);

		PropertyValuesHolder XHolder = PropertyValuesHolder.ofFloat(View.X, startBounds.left, finalBounds.left);
		PropertyValuesHolder YHolder = PropertyValuesHolder.ofFloat(View.Y, startBounds.top, finalBounds.top);
		PropertyValuesHolder scaleXHolder = PropertyValuesHolder.ofFloat(View.SCALE_X, startScale, 1.0f);
		PropertyValuesHolder scaleYHolder = PropertyValuesHolder.ofFloat(View.SCALE_Y, startScale, 1.0f);
		anim = ObjectAnimator.ofPropertyValuesHolder(imageView, XHolder, YHolder, scaleXHolder, scaleYHolder);
		anim.setDuration(500);
		anim.setInterpolator(new DecelerateInterpolator());
		anim.addListener(new AnimatorListenerAdapter() {
			@Override
			public void onAnimationEnd(Animator animation) {
				Log.d("cc", "end : " + imageView.getScaleX() + ", " + imageView.getScaleY());
			}
		});
		anim.start();
	}

	@Override
	public void onClick(View v) {
		Log.d("cc", "onClick()");
		anim.addListener(new AnimatorListenerAdapter() {
			@Override
			public void onAnimationEnd(Animator animation) {
				imageView.setVisibility(View.GONE);
			}
		});
		anim.reverse();

	}

}
